<template>
	<view class="baseView" v-model="personInfo">
		<view class="myflexRow" @click="onAvatarClick" style="margin-left: 16rpx;">
			<view>头像</view>
			<u-image :src="personInfo.avatar" shape="circle" width="100rpx" height="100rpx">
				<u-loading slot="loading"></u-loading>
			</u-image>
		</view>
		<u-action-sheet :list="list" v-model="show" @click="goToItem"></u-action-sheet>
		<u-form :model="personInfo" ref="uForm" style="margin-left: 16rpx;">
			<u-form-item label="姓名"><u-input v-model="personInfo.userName"></u-input></u-form-item>
			<u-form-item label="身份证" label-width="150"><u-input v-model="personInfo.idCard"></u-input></u-form-item>
			<u-form-item label="手机"><u-input v-model="personInfo.phonenumber"></u-input></u-form-item>
		</u-form>
	</view>
</template>

<script>
export default {
	data() {
		return {
			personInfo: {},
			list: [
				{
					text: '拍照'
				},
				{
					text: '相册'
				}
			],
			show: false
		};
	},
	methods: {
		onAvatarClick: function() {
			this.show = true;
		},

		//从相册选择
		showChooseImage: function() {
			return new Promise((resolve, reject) => {
				uni.chooseImage({
					count: 1,
					sourceType: ['album'],
					success: function(res) {
						resolve(res.tempFilePaths[0]);
					}
				});
			});
		},
		//拍照选择
		showGotoCamera: function() {
			return new Promise((resolve, reject) => {
				uni.chooseImage({
					count: 1,
					sourceType: ['camera'],
					success: function(res) {
						resolve(res.tempFilePaths[0]);
					}
				});
			});
		},
		//上传文件
		upLoadFile: function(path) {
			return new Promise((resolve, reject) => {
				if (!Object.is(path, '')) {
					uni.uploadFile({
						url: getApp().globalData.upLoadPath,
						filePath: path,
						name: 'file',
						header: {
							Authorization: getApp().globalData.token
						},
						success: uploadFileRes => {
							//巨坑,回来的数据是json，需要解析
							let parseData = JSON.parse(uploadFileRes.data);
							resolve(parseData);
						},
						fail: err => {
							console.log(err);
						}
					});
				} else {
					console.log('尚未选择照片');
				}
			});
		},
		goToItem: async function(index) {
			let tempData = {};
			switch (index) {
				//拍照
				case 0: {
					let path = await this.showGotoCamera();
					let upLoadresult = await this.upLoadFile(path);
					tempData.avatar = upLoadresult.fileName;
					let modifyResult = await this.modifyPersonInfo(tempData);
					let info = await this.$u.api.getPersonInfo();
					break;
				}
				case 1: {
					//相册
					let path = await this.showChooseImage();
					let result = await this.upLoadFile(path);
					tempData.avatar = result.fileName;
					console.log(tempData.avatar);
					let modifyResult = await this.modifyPersonInfo(tempData);
					let info = await this.$u.api.getPersonInfo();
					info.avatar = getApp()
						.globalData.baseUrl.concat('/prod-api/')
						.concat(info.avatar);
					this.personInfo = info;
					getApp().globalData.person = info;
					break;
				}
			}
		},
		//修改个人信息，注意已经是object的话不需要
		modifyPersonInfo(object) {
			return new Promise((resolve, reject) => {
				this.$u.put('/prod-api/api/common/user', object).then(res => {
					resolve(res);
				});
			});
		}
	},
	onLoad() {
		console.log('=====');
		this.personInfo = getApp().globalData.person;
	}
};
</script>

<style></style>
